<include file="Admin@Public/header" /> 
<?php  require './Config/home_param_config.php'; ?>
<div class="col-sm-10 col-sm-offset-3 col-md-10 col-md-offset-2 main">
    <ol class="breadcrumb">
      <li>网站设置</li>
      <li>内容设置</li>
      <li class="active">编辑</li>
    </ol>
<form  class="form-horizontal" role="form" action="__URL__/save" >
  <div class="table-responsive">
         <table class="table">
              <thead>
                <tr>
                  <th class="col-md-1">#</th>
                  <th class="col-md-2">首页版块</th>
                  <th class="col-md-1">类型</th>
                  <th class="col-md-6" colspan="2">内容</th>
                  <th class="col-md-2">操作</th>
                </tr>
              </thead>
            <tbody>
            <volist name="home_param_list" id="vo">
              <tr>
                <td>{$i}</td>
                <td>{$vo.title}</td>
                <td>
                    <switch name="vo.custom">
                      <case value="article">文章</case>
                      <case value="type">栏目</case>
                    </switch>
                </td>
                <td><input type="text" class="form-control" id="{$vo.param}" name="{$vo.param}" datatype="content_site" sucmsg="<i class='icon-ok'></i>" value="<?php $param=$vo['param']; echo $content[$param];?> "></td><td class="col-md-2"></td>
                <td><a class="btn btn-default btn-xs" href="#" data-param="{$vo.param}" data-whatever="{$vo.custom}"  data-toggle="modal" data-target="#myModal"><i class="icon-edit"></i></a></td>
              </tr>
            </volist>  
            </tbody>
            <tfoot>
              <tr class="info">
               <td colspan="5">注：内容是对应的ID号，之间用英文逗号","隔开</td><td><input type="button" class="btn btn-primary" value="保存" id="btn_sub"/></td>
              </tr>
            </tfoot> 
          </table>
      </div>
</form> 

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="false">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel"></h4>
      </div>
      <div class="modal-body">
        <table class="table">
              <thead>
                <tr>
                  <th class="col-md-1">#</th>
                  <th class="col-md-9" id="tab-head-title">标题</th>
                </tr>
              </thead>
            <tbody id="tab-body">
            </tbody>
          </table>
      </div>
      <div class="modal-footer">
        <input type="hidden" id="" value="">
        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
        <button type="button" class="btn btn-primary" onclick="saveChecked();">确定</button>
      </div>
    </div>
  </div>
</div>
</div>
<script type="text/javascript">
var param;
Array.prototype.contains = function (obj) {  
    var i = this.length;  
    while (i--) {  
        if (this[i] === obj) {  
            return true;  
        }  
    }  
    return false;  
} 
  /*网站首页设置 弹窗*/
$('#myModal').on('show.bs.modal', function (event) {
  var button = $(event.relatedTarget); // Button that triggered the modal
  param = button.data('param');
  var checkedIdStr = $('#'+param).val().replace(/\s+/g,"");
  var checkedIdArr = checkedIdStr.split(","); 
  var recipient = button.data('whatever'); // Extract info from data-* attributes
  var recipientTitle;
  switch(recipient){
    case "article":
      recipientTitle="文章";
      tableStr = articleShow(checkedIdArr);
      break;
     case "type":
      recipientTitle="栏目";
      tableStr = typeShow(checkedIdArr);
      break;  
  } 
  var modal = $(this);
  modal.find('.modal-title').text('选择' + recipientTitle);
  modal.find('#tab-head-title').html(recipientTitle);
  modal.find('#tab-body').html(tableStr);
  $('input').iCheck({checkboxClass: 'icheckbox_square-blue',radioClass: 'iradio_square-blue',increaseArea: '20%'});
});

function saveChecked()
{
  var ids= getDbids();
  $('#'+param).val(ids);
  $('#myModal').modal('hide');
}
 function getDbids(){
  var arrId=new Array();
  $('input[type=checkbox]:checked').each(function(num){
      var id = $(this).val();
      if(id!=null&&id!=""){
        rid = id.replace("/&code=/","|");
        arrId.push(rid);
      }
  });
  return arrId.join(",");
}

function articleShow(checkedIdArr){
  var tableStr="";
  $.ajax({
       url:"/Content/Myajax/getArticles",
       type:"get",
       async:false,
       dataType:'json',
       success:function(data){
          if (data.status) {
            var dataArr=data.dataValue;
            if (dataArr) {
              for (var i = 0; i <dataArr.length ; i++) {
                  var id=dataArr[i]['id'];
                  var title=dataArr[i]['title'];
                  var checked=(checkedIdArr.contains(id))? "checked" : "";
                  tableStr+="<tr><td><input type='checkbox' name='checkbox_id' value='"+id+"'"+checked+"></td><td>"+title+"</td></tr>";
              };
            };
          }else{
            tableStr=data.msg;
          }
        },
       error:function(e){
           tableStr = "数据错误";
        }
    });

    return  tableStr; 
};
function typeShow(checkedIdArr){
  var tableStr="";
    $.ajax({
       url:"/Content/Myajax/getTypes",
       type:"get",
       async:false, 
       dataType:'json',
       success:function(data){
          if (data.status) {
            var dataArr=data.dataValue;
            if (dataArr) {
              for (var i = 0; i <dataArr.length ; i++) {
                  var id=dataArr[i]['id'];
                  var pdlf =dataArr[i]['sort']*20;
                  var name="<span style='padding-left:"+pdlf+"px'>"+dataArr[i]['html']+dataArr[i]['name']+"</span>";
                  var checked=(checkedIdArr.contains(id))? "checked" : "";
                  tableStr+="<tr><td><input type='checkbox' name='checkbox_id' value='"+id+"'"+checked+"></td><td>"+name+"</td><td></td></tr>";
              };
            };
          }else{
            tableStr=data.msg;
          }
        },
       error:function(e){
           tableStr = "数据错误";
        }
    });
    return  tableStr;
}
</script>
<include file="Admin@Public/script_form" /> 
<include file="Admin@Public/footer" /> 